package com.cz.ui.chat;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.cz.R;
import com.cz.adapter.chat.ChatAdapter;
import com.cz.pojo.ChatRecord;
import com.cz.utils.DBOpenHelper;

import java.util.ArrayList;

public class ChatList extends Fragment {

    private DBOpenHelper dbOpenHelper;
    private ListView chatList;
    private  ArrayList<ChatRecord> chat;
    private ChatRecord chatRecord1;

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View root = inflater.inflate(R.layout.fragment_chat_list, container, false);
        chatList = root.findViewById(R.id.listView_chat);
        return root;
    }


    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        selectDatabase();
    }

    /**
     * 数据库查询
     */
    private void selectDatabase() {
        dbOpenHelper = new DBOpenHelper(getActivity(),"WeChatMini",null,2);
        SQLiteDatabase db = dbOpenHelper.getReadableDatabase();

        chat = new ArrayList<>();

        insertInfo2(db);


        //满足条件查询
        Cursor user = db.rawQuery("select * from dialoguelist", null);
        while(user.moveToNext()) {

            String listid = user.getString(0);
            String userid = user.getString(2);
            String endtime = user.getString(4);
            String endmessage = user.getString(1);
            String headimg = user.getString(3);
            System.out.println(headimg);
            chatRecord1 = new ChatRecord(listid,userid,endtime,endmessage,headimg);
            //查出数据循环加入
            chat.add(chatRecord1);
        }

        ChatAdapter chatAdapter = new ChatAdapter(getActivity(), chat);
        chatList.setAdapter(chatAdapter);

        ListClickListener l = new ListClickListener();
        chatList.setOnItemClickListener(l);

        db.close();
    }

    //点击事件监听类
    class ListClickListener implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // TODO Auto-generated method stub
            //通过view获取组件中的nickName
            String nickName = ((TextView) view.findViewById(R.id.tv_chat_name)).getText().toString();
            //进行页面跳转
            Intent intent = new Intent(parent.getContext(), ChatInfo.class);
            intent.putExtra("nickName",nickName);
            startActivity(intent);
        }
    }

    //用于数据持久化(插入方法只执行一次 ,多次执行将会报错,执行一次成功后请删除此方法)
    private void insertInfo2(SQLiteDatabase db) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("listid","1");
        contentValues.put("endmessage","今天晚上吃什么");
        contentValues.put("userid","BiShuGui");
        contentValues.put("headimg","bisugui");
        contentValues.put("endtime","16:00");
        long mine = db.insert("dialoguelist",null, contentValues);
    }
}